<?php

declare(strict_types=1);

namespace App\Robot\Service;

use App\Common\Robot\Contracts\GatewayInterface;
use App\Common\Robot\Gateways\Gateway;
use App\Common\Robot\Robot;
use App\CustHub\Dto\AddMsgTemplateDto;
use App\CustHub\Mapper\ChRobotMapper;
use App\CustHub\Service\OpenWorkService;
use Mine\Abstracts\AbstractService;
/**
 * 极速群发
 * Class RobotMessageService
 * @package App\Robot\Service
 */
class SpeedMessageService extends AbstractService
{
    protected AddMsgTemplateDto $data;

    protected OpenWorkService $OpenWorkService;

    protected GatewayInterface $RobotApiService;

    public function __construct(AddMsgTemplateDto $data,string $authCorpId,int $robotId)
    {
        $this->OpenWorkService=new OpenWorkService($authCorpId);
        $robot = container()->get(ChRobotMapper::class)->findById($robotId);
        $this->RobotApiService= make(Robot::class,[$robot->server_id,$robot->uuid])->gateway();
        $this->data = $this->convertSenderIds($data,$robotId);
    }

    public function sendMessage()
    {
        var_dump('极速群发发送时间-----'.datetime(time()));
       $result=$this->OpenWorkService->addMsgTemplate(
           $this->data
       );
       if($result){
           return $result['msgid'];
       }
       return false;
    }


    /**
     *转换发送的id为可企业微信可用id
     */
    private function convertSenderIds(AddMsgTemplateDto $data,$robotId)
    {
        $chatType=$data->getChatType();
        if($chatType=='group'){
            //转换群id
            $newChatIds=[];
            foreach ($data->getChatIdList() as $chatId){
                $result=$this->RobotApiService->roomidToOpenroomid($chatId);
                $newChatIds[]=$result['open_room_id'];
            }
            $data->setChatIdList($newChatIds);
        }else{
            //转换用户id
            $result = $this->RobotApiService->useridToOpenuserid($data->getExternalUserid());
            $open_user_ids=$this->OpenWorkService->getNewExternalUserid(array_column($result,'open_user_id'));
            $data->setExternalUserid(array_column($open_user_ids,'new_external_userid'));
        }
        $attachments=$data->getAttachments();
        $newAttachments=[];
        foreach ($attachments as $attachment){
            $newAttachment=[];
            switch ($attachment['msg_type']){
                case "image":
                    $result=$this->OpenWorkService->uploadMedia($attachment['content'],'image');
                    $result && $newAttachment=[
                        "msgtype"=>"image",
                        "image"=>["media_id"=>$result['media_id']],
                    ];
                    break;
                case "url":
                    $newAttachment=[
                        "msgtype"=>"link",
                        "link"=>[
                            "title"=>$attachment['content']['title'],
                            "picurl"=>$attachment['content']['cover'],
                            "desc"=>$attachment['content']['desc'],
                            "url"=>$attachment['content']['url'],
                        ],
                    ];
                    break;
                case "mini_program":
                    $result=$this->OpenWorkService->uploadMedia($attachment['content']['icon_url'],'image');
                    $result && $newAttachment=[
                        "msgtype"=>"miniprogram",
                        "miniprogram"=>[
                            "title"=>$attachment['content']['title'],
                            "pic_media_id"=>$result['media_id'],
                            "appid"=>$attachment['content']['appid'],
                            "page"=>$attachment['content']['pagepath'],
                        ],
                    ];
                    break;
                case "video":
                    $result=$this->OpenWorkService->uploadMedia($attachment['content'],'video');
                    $result && $newAttachment=[
                        "msgtype"=>"video",
                        "video"=>[
                            "media_id"=>$result['media_id']
                        ],
                    ];
                    break;
                case "file":
                    $result=$this->OpenWorkService->uploadMedia($attachment['content'],'file');
                    $result && $newAttachment=[
                        "msgtype"=>"file",
                        "file"=>[
                            "media_id"=>$result['media_id']
                        ],
                    ];
                    break;
            }

            if($newAttachment){
                $newAttachments[]=$newAttachment;
            }
        }
        $data->setAttachments($newAttachments);
        return $data;
    }
}